package innodb;

import com.alibaba.innodb.java.reader.TableReader;
import com.alibaba.innodb.java.reader.TableReaderImpl;
import com.alibaba.innodb.java.reader.comparator.ComparisonOperator;
import com.alibaba.innodb.java.reader.page.AbstractPage;
import com.alibaba.innodb.java.reader.page.PageType;
import com.alibaba.innodb.java.reader.page.fsphdr.FspHdrXes;
import com.alibaba.innodb.java.reader.page.index.GenericRecord;
import com.alibaba.innodb.java.reader.page.index.Index;
import com.alibaba.innodb.java.reader.page.inode.Inode;
import com.google.common.collect.ImmutableList;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import static com.alibaba.innodb.java.reader.page.PageType.*;
import static com.alibaba.innodb.java.reader.page.PageType.INDEX;

/**
 * @author dushougudu
 */
public class DbReader {
    public static void main(String[] args) {
        String createTableSql = "CREATE TABLE user (\n" +
                "  id int NOT NULL AUTO_INCREMENT,\n" +
                "  name varchar(25) COLLATE utf8mb4_bin DEFAULT NULL,\n" +
                "  age int DEFAULT NULL,\n" +
                "  PRIMARY KEY (id),\n" +
                "  KEY id_age (age)\n" +
                ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;\n" +
                "\n";
        String ibdFilePath = "D:\\ideawork\\spring-demo\\tool-example\\src\\main\\resources\\user.ibd";
        try (TableReader reader = new TableReaderImpl(ibdFilePath, createTableSql)) {
            reader.open();

            // ~~~ query by sk, projection
            Iterator<GenericRecord> iterator = reader.getRecordIteratorBySk("id_age",
                    ImmutableList.of(1), ComparisonOperator.GTE,
                    null, ComparisonOperator.NOP, ImmutableList.of("name"));
            while (iterator.hasNext()) {
                GenericRecord record = iterator.next();
                Object[] values = record.getValues();
                System.out.println(Arrays.asList(values));
            }

            // ~~~ query all records
//            Iterator<GenericRecord> iterator = reader.getQueryAllIterator();
//            while (iterator.hasNext()) {
//                GenericRecord record = iterator.next();
//                Object[] values = record.getValues();
//                System.out.println(Arrays.asList(values));
//                assert record.getPrimaryKey() == record.get("id");
//                System.out.println("id=" + record.get("id"));
//                System.out.println("name=" + record.get("name"));
//                System.out.println("age=" + record.get("age"));
//                System.out.println("pageNum=" + record.getPageNumber());
//            }
//            AbstractPage page = reader.readPage(4);
//            System.out.println(page.toString());


            List<AbstractPage> pages = reader.readAllPages();
            for (AbstractPage page : pages) {
                StringBuilder sb = new StringBuilder();
                sb.append(page.getPageNumber());
                sb.append(",");
                sb.append(page.pageType().name());
                if (FILE_SPACE_HEADER.equals(page.pageType()) || EXTENT_DESCRIPTOR.equals(page.pageType())) {
                    sb.append(",numPagesUsed=");
                    sb.append(((FspHdrXes) page).getFspHeader().getNumberOfPagesUsed());
                    sb.append(",size=");
                    sb.append(((FspHdrXes) page).getFspHeader().getSize());
                    sb.append(",xdes.size=");
                    sb.append(((FspHdrXes) page).getXdesList().size());
                } else if (INODE.equals(page.pageType())) {
                    sb.append(",inode.size=");
                    sb.append(((Inode) page).getInodeEntryList().size());
                } else if (INDEX.equals(page.pageType()) && !((Index) page).isLeafPage()) {
                    sb.append(",level=");
                    sb.append(((Index) page).getIndexHeader().getPageLevel());
                    sb.append(",numOfRecs=");
                    sb.append(((Index) page).getIndexHeader().getNumOfRecs());
                } else {
                    //continue;
                }
                sb.append(page.toString());
                System.out.println(sb.toString());
            }

        }


    }
}
